﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using prjBULHRM;
namespace prjQuanLyNhanSu
{
    public partial class frmBangCap : Form
    {
        public frmBangCap()
        {
            InitializeComponent();
        }
        clsBULHT_BangCap cls;
        public void LoadDuLieu()
        {
            cls = new clsBULHT_BangCap();
            dgrBangCap.DataSource = cls.SelectAll();
            txtMaBC.Enabled = false;
            txtTenBC.Enabled = false;            
        }
        
        private void frmBangCap_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
        }

        private void dgrBangCap_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaBC.Enabled = false;
            txtTenBC.Enabled = false;

            btnThem.Text = "&Thêm";
            btnSua.Text = "&Sửa";
        }

        private void dgrBangCap_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaBC.Text = dgrBangCap.CurrentRow.Cells[0].Value.ToString();
            txtTenBC.Text = dgrBangCap.CurrentRow.Cells[1].Value.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "&Thêm")
            {
                btnThem.Text = "&Lưu Thêm";
                txtMaBC.Text = "";
                txtTenBC.Text = "";
                txtMaBC.Enabled = true;
                txtTenBC.Enabled = true;
                txtMaBC.Focus();

                btnSua.Enabled = false;
                btnXoa.Enabled = false;
            }
            else
            {
                if (txtMaBC.Text == "" || txtTenBC.Text == "")
                {
                    MessageBox.Show("Thông tin chưa đầy đủ!", "Thiếu Dữ Liệu.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    cls = new clsBULHT_BangCap();
                    btnThem.Text = "&Thêm";
                    cls.BangCapID = txtMaBC.Text;
                    cls.TenBangCap = txtTenBC.Text;
                    if (cls.TrungMa() == true)
                    {
                        MessageBox.Show("Trùng mã", "Lỗi Trùng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        cls.Insert();
                        LoadDuLieu();
                        MessageBox.Show("Thêm thành công!");
                    }
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
           
                if (btnSua.Text == "&Sửa")
                {
                     if (txtMaBC.Text == "")
                        {
                            MessageBox.Show("Bạn chưa chọn dữ liệu để sửa.");
                        }
                        else
                        {                     
                            btnSua.Text = "&Lưu Sửa";
                            txtMaBC.Enabled = false;
                            txtTenBC.Enabled = true;
                            btnThem.Enabled = false;
                            btnXoa.Enabled = false;
                         }
                }
                else
                {
                    try
                    {
                        btnSua.Text = "&Sửa";
                        cls = new clsBULHT_BangCap();
                        cls.BangCapID = txtMaBC.Text;
                        cls.TenBangCap = txtTenBC.Text;
                        if (txtMaBC.Text == "" || txtTenBC.Text == "")
                        {
                            MessageBox.Show("Không được rỗng!");
                        }
                        else
                        {
                            cls.Update();
                            LoadDuLieu();
                            MessageBox.Show("Sửa thành công!");
                            btnThem.Enabled = true;
                            btnXoa.Enabled = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Dữ iệu này không sửa được.");
                    }
                }            
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaBC.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu dể xóa.");
            }
            else
            {
                DialogResult hoi = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này không?","Xóa dữ liệu",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if (hoi == DialogResult.Yes)
                {
                    try
                    {
                        cls = new clsBULHT_BangCap();
                        cls.BangCapID = txtMaBC.Text;
                        cls.BangCapID = dgrBangCap.CurrentRow.Cells[0].Value.ToString();
                        cls.Delete();
                        LoadDuLieu();
                        MessageBox.Show("Xóa thành công!");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Dữ liệu này không xóa được!");
                    }
                }
            }
        }
    }
}
